#!/bin/sh
# Stop running karaf service before removing any files

set -e

stop_karaf_service() {
    if systemctl stop karaf.service
    then
       echo "Stopped karaf service"
    else
       echo "Failed to stop karaf service"
    fi

    # wait for karaf service to stop before proceeding
    sleep 2
}

remove_karaf_osgi_cache_pidfile_lock_file_and_instances() {
    rm -rf /var/lib/karaf/data/cache
    rm -f /var/lib/karaf/karaf.pid
    rm -f /var/lib/karaf/lock
    rm -rf /var/lib/karaf/instances
}

case "$1" in
    remove|purge|upgrade|deconfigure)
        stop_karaf_service
        remove_karaf_osgi_cache_pidfile_lock_file_and_instances
    ;;
esac

#DEBHELPER#

exit 0
